package com.atoffice.auth.controller;


import com.atoffice.auth.service.SysRoleService;
import com.atoffice.common.config.exception.CustomException;
import com.atoffice.common.result.Result;
import com.atoffice.model.system.SysRole;
import com.atoffice.vo.system.SysRoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
    //http://localhost:8800/admin/system/sysRole/findAll


    //注入Service
    @Autowired
    private SysRoleService sysRoleService;
/**
    测试学习用
    //查询所有角色
    @GetMapping("/findAll")
    public List<SysRole> findAll() {
        //调用Service的方法
        List<SysRole> list = sysRoleService.list();
        return list;
    }

 */
    //统一返回数据结果
    @ApiOperation("查询所有角色")
    @GetMapping("/findAll")
    public Result findAll() {
        //调用Service的方法
        List<SysRole> list = sysRoleService.list();

        //模拟异常
        try {
            int i = 10/0;
        } catch (Exception e){
            //抛出自定义异常
            throw new CustomException(10086 , "执行了自定义异常");
        }
        return Result.success(list);
    }


    //条件分页查询
    //page：当前页 ； limit：每页记录数
    @ApiOperation("条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result pageQueryRole(@PathVariable Long page , @PathVariable Long limit , SysRoleQueryVo sysRoleQueryVo){
        //调用service方法实现
        //1.创建page对象，传递分页相关参数
        Page<SysRole> pageParm = new Page<>(page , limit);
        //2.封装条件，判断条件是否为空，不为空进行封装
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        String roleName = sysRoleQueryVo.getRoleName();
        if (!StringUtils.isEmpty(roleName)){

            //不为空进行封装
            wrapper.like(SysRole::getRoleName , roleName);//like:模糊查询 ; equlas: 完全匹配
        }
        //3.调用方法实现
        IPage<SysRole> pageModel = sysRoleService.page(pageParm , wrapper);
        return Result.success(pageModel);
    }

    //添加角色
    @ApiOperation("添加角色")
    @PostMapping("save")
    public Result save(@RequestBody SysRole role){ //@RequestBody已请求体josn格式传递参数；不过请求体这样无法使用Get提交
        //调用service方法实现
        boolean is_success = sysRoleService.save(role);
        if (is_success){
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    //根据id查询
    @ApiOperation("根据id查询")
    @GetMapping("get/{id}")
    public Result get (@PathVariable Long id){
        SysRole sysRole = sysRoleService.getById(id);
        return Result.success(sysRole);
    }

    //修改角色
    @ApiOperation("修改角色")
    @PutMapping("update")
    public Result update(@RequestBody SysRole role){ //@RequestBody已请求体josn格式传递参数；不过请求体这样无法使用Get提交
        //调用service方法实现
        boolean is_success = sysRoleService.updateById(role);
        if (is_success){
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    //根据id删除
    @ApiOperation("根据id删除")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id){
        boolean is_success = sysRoleService.removeById(id);
        if (is_success){
            return Result.success();
        } else {
            return Result.fail();
        }
    }

    //批量删除
    //前段传入josn数组格式
    @ApiOperation("批量删除")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList){
        boolean is_success = sysRoleService.removeByIds(idList);
        if (is_success){
            return Result.success();
        } else {
            return Result.fail();
        }
    }

}
